﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SGArchiveSystem7xDB;
using SGArchiveSystem7xDB.Linq;

namespace SGArchiveSystem7x.Controllers.ArchiveTree
{
    public class ArchvieTreeController : BaseController
    {
        /// <summary>
        /// 本控制器的名称，用于权限访问 (档案权限)
        /// </summary>
        private string sControllerName = "ArchiveTree";

        /// <summary>
        /// 档案树界面
        /// </summary>
        /// <returns></returns>
        ///[HttpPost]
        //[ValidateAntiForgeryToken]
        // GET: ArchvieTree
        public ActionResult Index()
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;

            ///清除Page级的Session
            App_Code.CSession.Clear_PageSession(Session);

           

            ///获取档案大类
            var listType = SGArchiveSystem7xDB.Linq.T_ArchiveTypeDal.Get_ArchiveType();
            ///获取档案类型
            var listTree = SGArchiveSystem7xDB.Linq.T_ArchiveTreeDal.Get_ArchiveTree(-1, 1);

            var listArchiveTree = Models.CArchiveTreeModel.Create_ArchiveTree(listType, listTree);
            ViewBag.ArchiveTree = listArchiveTree;

            #region 测试建立档案类型 ( 测试通过）
            /*
            ///NO1. 建立档案类型
            var en = new T_ArchiveType() { ATYPE_NAME = "档案分类", ATYPE_ORDER = 1 };
            var value = T_ArchiveTypeDal.Add_ArchiveType(en);
            if(value == _ReturnValue.OK)
            {
                var en2 = new T_ArchiveTree()
                {
                    AR_AJTABLE = "T_ArchiveAj",
                    AR_LASTDATEIME = DateTime.Now,
                    AR_LASTUSERNAME = "admin",
                    AR_JNTABLE = "",
                    AR_NAME = "文书档案",
                    AR_ORDER = 1,
                    AR_PID = 0,
                    AR_TYPEID = en.ATYPE_ID,
                    AR_STATUS = 1
                };

                if(T_ArchiveTreeDal.Add_ArchiveTree(en2) == _ReturnValue.OK)
                {
                    T_ArchiveFieldsDal.Add_ArchiveFieldsInit(en2.AR_ID, en2.AR_LASTUSERNAME);
                }
            }*/
            #endregion

            #region 测试建立单个字段（测试通过）
            T_ArchiveFields en = new T_ArchiveFields();
            en.AF_EDIT = 1;
            en.AF_AJORJN = 1;
            en.AF_ISNULL = 1;
            en.AF_LASTDATETIME = DateTime.Now;
            en.AF_LASTUSERNAME = "admin";
            en.AF_LEN = 50;
            en.AF_NAME = "测试字段";
            en.AF_ORDER = 5;
            en.AF_PID = 4;
            en.AF_QUERY = 1;
            en.AF_SHOW = 1;
            en.AF_STAT = 1;
            en.AF_STATUS = 1;
            en.AF_SYSTEM = 1;
            en.AF_TYPE = "文本";
            en.AF_WORDID = 0;
            T_ArchiveFieldsDal.Add_ArchiveFields(en);

            #endregion

            return View();
        }

        /// <summary>
        /// 添加档案大类
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        public string Save_ajaxArchiveType(int id, string name)
        {
            if (id == 0)
            {
                int value = (int)SGArchiveSystem7xDB.Linq.T_ArchiveTypeDal.Add_ArchiveType(new T_ArchiveType() { ATYPE_NAME = name });
                return value.ToString();
            }
            else
            {
                int value = (int)SGArchiveSystem7xDB.Linq.T_ArchiveTypeDal.Modify_ArchiveType(id, name);
                return value.ToString();
            }
        }

        /// <summary>
        /// 添加档案类型
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public string Save_ajaxArchiveTree(int id, int typeid, string name)
        {
            var user = SGArchiveSystem7x.App_Code.CSession.Get_User(Session);
            if (id == 0)
            {
                var en = new T_ArchiveTree()
                {
                    AR_AJTABLE = "T_ArchiveAj",
                    AR_LASTDATEIME = DateTime.Now,
                    AR_LASTUSERNAME = user.U_RNAME,
                    AR_JNTABLE = "",
                    AR_NAME = name,
                    AR_ORDER = 1,
                    AR_PID = 0,
                    AR_TYPEID = typeid,
                    AR_STATUS = 1
                };
                var value = T_ArchiveTreeDal.Add_ArchiveTree(en);
                if(value == _ReturnValue.OK)
                {
                    value = T_ArchiveFieldsDal.Add_ArchiveFieldsInit(en.AR_ID, en.AR_LASTUSERNAME);
                    return value.ToString();
                }
                else
                    return value.ToString();
            }
            else
            {
                int value = (int)SGArchiveSystem7xDB.Linq.T_ArchiveTreeDal.Modify_ArchiveTree(id, name);
                return value.ToString();
            }
        }

        /// <summary>
        /// 档案类型
        /// </summary>
        /// <param name="arid"></param>
        /// <returns></returns>
        public ActionResult ArchiveFields(int arid, string title)
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;

            ///清除Page级的Session
            App_Code.CSession.Clear_PageSession(Session);

            if (Models.Language._nowLanguage == _VersionLanguage._Chs)
            {
                ViewBag.Text0 = "物品字段";
                ViewBag.Text1 = "添加字段";
                ViewBag.Text2 = "添加类型";
                ViewBag.Text3 = "修改类型";
                ViewBag.Text4 = "编辑字段";
                ViewBag.Text5 = "字段名称:";
                ViewBag.Text6 = "字段类型:";
                ViewBag.Text7 = "是否显示:";
                ViewBag.Text8 = "是否查询:";
                ViewBag.Text9 = "是否编辑:";
                ViewBag.Text10 = "显示长度:";
                ViewBag.Text11 = "排序位置:";
                ViewBag.Text12 = "添加";
                ViewBag.Text13 = "修改";
                ViewBag.Text14 = "取消";
                ViewBag.Text15 = "编辑类型";
                ViewBag.Text16 = "分类:";
                ViewBag.Text17 = "物品名称:";
                ViewBag.Text18 = "启用";
                ViewBag.Text19 = "关闭";
                ViewBag.Text20 = "编辑";
                ViewBag.Text21 = "提示";
                ViewBag.Text22 = "类型名称不能为空!";
                ViewBag.Text23 = "重复的类型名称!";
                ViewBag.Text24 = "保存失败";
                ViewBag.Text25 = "长度或排序必须为数字!";
                ViewBag.Text26 = "文本";
                ViewBag.Text27 = "重复字段名称!";
                ViewBag.Text28 = "修改成功!";
                ViewBag.Text29 = "添加成功!";
                ViewBag.Text30 = "词典";
            }
            else
            {
                ViewBag.Text0 = "Item Field";
                ViewBag.Text1 = "Add Field";
                ViewBag.Text2 = "Add Type";
                ViewBag.Text3 = "Edit Type";
                ViewBag.Text4 = "Edit Field";
                ViewBag.Text5 = "Field Name:";
                ViewBag.Text6 = "Field Type:";
                ViewBag.Text7 = "Display:";
                ViewBag.Text8 = "Query:";
                ViewBag.Text9 = "Edit:";
                ViewBag.Text10 = "Show Len:";
                ViewBag.Text11 = "Short:";
                ViewBag.Text12 = "Add";
                ViewBag.Text13 = "Modify";
                ViewBag.Text14 = "Cancel";
                ViewBag.Text15 = "Edit Type";
                ViewBag.Text16 = "classify:";
                ViewBag.Text17 = "Name:";
                ViewBag.Text18 = "Yes";
                ViewBag.Text19 = "No";
                ViewBag.Text20 = "Edit";
                ViewBag.Text21 = "Tip";
                ViewBag.Text22 = "name cannot be empty";
                ViewBag.Text23 = "already have the same name";
                ViewBag.Text24 = "Save failed";
                ViewBag.Text25 = "Length or sorting must be numbers";
                ViewBag.Text26 = "Text";
                ViewBag.Text27 = "already have the same field name";
                ViewBag.Text28 = "Save successfully";
                ViewBag.Text29 = "Save successfully";
                ViewBag.Text30 = "WORD";
            }

            var listColumns = Models.CColumnsModel.Create_ArchiveFieldsColumns();
            ViewBag.Columns = listColumns;
            ViewBag.Title = title;
            ViewBag.ARID = arid;

            var listType = SGArchiveSystem7xDB.Linq.T_ArchiveTypeDal.Get_ArchiveType();
            ViewBag.ArchieType = listType;

            var listWordType = Models.CComboboxModel.Create_ComboboxWordTypeTree(false);
            ViewBag.WordType = listWordType;
            return View();
        }

        public ActionResult Get_ajaxArchiveFields(int arid)
        {
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_ArchiveFieldsDal.Get_ArchiveFields(out count, 1, arid, -1, -1, -1, -1, -1);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 根据ID获取单个的字段信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Get_ajaxArchiveFieldsSingle(int id)
        {
            var en = T_ArchiveFieldsDal.Get_ArchiveFields(id);
            var json = new
            {
                en
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 保存字段
        /// </summary>
        /// <returns></returns>
        public string Save_ajaxArchiveFields(int aid, string name, string type, int wordid, 
            int show, int query, int edit, int stat, int isnull, int len, int order, int id)
        {
            T_ArchiveFields en = new T_ArchiveFields();
            en.AF_PID = aid;
            en.AF_NAME = name;
            en.AF_TYPE = type;
            en.AF_AJORJN = 1;
            en.AF_WORDID = wordid;
            en.AF_SHOW = show;
            en.AF_QUERY = query;
            en.AF_EDIT = edit;
            en.AF_STAT = stat;
            en.AF_SYSTEM = 0;
            en.AF_ORDER = order;
            en.AF_ISNULL = isnull;
            en.AF_LASTDATETIME = DateTime.Now;
            en.AF_LASTUSERNAME = "111";
            en.AF_STATUS = 1;
            en.AF_LEN = len;
            if(id == 0)
                return (T_ArchiveFieldsDal.Add_ArchiveFields(en)).ToString();
            else
            {
                en.AF_ID = id;
                return (T_ArchiveFieldsDal.Modify_ArchiveFileds(en)).ToString();
            }
        }



    }
}